#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

int g_bowl=0;

pthread_mutex_t g_lock;
pthread_cond_t g_cond;

void* pthread_eat(void* arg){
    pthread_detach(pthread_self());
    while(1){
        pthread_mutex_lock(&g_lock);
        if(g_bowl<=0){
           pthread_cond_wait(&g_cond,&g_lock);
        }
        printf("i am eating the %d\n",g_bowl);
        g_bowl--;
        pthread_mutex_unlock(&g_lock);
        pthread_cond_signal(&g_cond);
    }
}
void* pthread_make(void* arg){
    pthread_detach(pthread_self());
    while(1){
        pthread_mutex_lock(&g_lock);
        if(g_bowl>0){
           pthread_cond_wait(&g_cond,&g_lock);
        }
        sleep(120);
        g_bowl++;
        printf("i am making the %d\n",g_bowl);
        pthread_mutex_unlock(&g_lock);
        pthread_cond_signal(&g_cond);
    }
}

int main(){
    pthread_mutex_init(&g_lock,NULL);
    pthread_cond_init(&g_cond,NULL);
    pthread_t tid;

    int ret =pthread_create(&tid,NULL,pthread_eat,NULL);
    if(ret<0){
        perror("pthread_create");
        return 0;
    }
    ret =pthread_create(&tid,NULL,pthread_make,NULL);
    if(ret<0){
        perror("pthread_create");
        return 0;
    }

    while(1){
    sleep(1);
    }
    pthread_mutex_destroy(&g_lock);
    pthread_cond_destroy(&g_cond);
    return 0;
}
